<?php
	namespace core;
	//封装自动事务
	class Transaction{
		private $service;
		function __construct($service){
			$this->service = $service;
		}
		
		//自动开启事务
		function __call($method, $arguments)
		{
		   if(!method_exists($this->service,$method)){
				echo "<br/>".get_class($this->service)."->$method function is not exist. ";
				exit;
		   }
		   global $APP_CONFIG;
		   if(!preg_match('/'.$APP_CONFIG['SYS_NO_TRANSACTION_POINTS'].'/',$method)){
				 //开启事务
				App::DB()->beginTransaction();
				try{
					$result = call_user_func_array(array($this->service,$method),$arguments);
					 //提交事务
					App::DB()->commit();
					return $result;
				}catch(\Exception $ex){
					try{App::DB()->rollBack();}catch(\Exception $ex2){};
					throw $ex;
				}
			}else{
				return call_user_func_array(array($this->service,$method),$arguments);
			}
		}
	}